﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Xml.XPath;

namespace HtmlExtractor.XPathOutput
{
    class OneManyCompositeProcessor : XmlDocOutputPathProcessor
    {
        public OneManyCompositeProcessor():base()
        {}
        public OneManyCompositeProcessor(IGrouping<List<string>, IPageItem> inputData)
            : base(inputData)
        { }
        public override bool GetResult(ref XDocument outputData)
        {
            base.GetResult(ref outputData);
            if (InputData.Key.Count > 1 && InputData.Count() > 1)
            {
                //key是唯一的compositepath
                CompositeOutputPath cpath = new CompositeOutputPath(InputData.Key);
                string compositePath = cpath.CompositePath;
               
                List<string> matchs = new List<string>();
                foreach (XPathNavigator compositeNav in Navigator.Select(compositePath, NameSpace))
                {
                    string singleComposite = "";
                    foreach (CompositeItem comItem in InputData.Cast<CompositeItem>())
                    {
                        XDocument compositeXml = XDocument.Parse(compositeNav.OuterXml);
                       // compositeXml.LoadXml(compositeNav.OuterXml);
                        XPathNavigator compositeNavTemp = compositeXml.CreateNavigator();
                        foreach (SimpleItem simple in comItem)
                        {
                            EvaluateXPath(ref compositeXml, simple.OutputPath, simple.Value);
                        }
                        singleComposite += compositeNavTemp.OuterXml;
                    }
                    matchs.Add(singleComposite);
                }
               // XDocument doc = (XDocument)Navigator.UnderlyingObject;
                XDocument xe = (XDocument)Navigator.UnderlyingObject;
                var a = xe.XPathSelectElements(compositePath, NameSpace).ToList();
                for (int k = 0; k < matchs.Count; k++)
                {
                    a[k].ReplaceWith(XmlHelper.ReadFragment(matchs[k]));
                }
                outputData = XDocument.Parse(xe.ToString());
                return true;
            }
            return false;
        }


        class CompositeOutputPath
        {
            /// <summary>
            /// 去掉composite部分的路径
            /// </summary>
            public List<string> XPaths = new List<string>();
            private List<XPathOutputPathParser> parsedXPaths = new List<XPathOutputPathParser>();
            public List<XPathOutputPathParser> ParsedXPaths
            { get { return parsedXPaths; } }
            public CompositeOutputPath(List<string> paths)
            {
                foreach (string p in paths)
                {
                    XPathOutputPathParser xp = new XPathOutputPathParser(p);
                    ParsedXPaths.Add(xp);
                    XPaths.Add(xp.XPath);
                    if (xp.XpathType == XpathType.Element)
                    {
                        elementPath.Add(p);
                    }
                    else
                    { attributePath.Add(p); }
                }
            }
            private List<string> attributePath = new List<string>();
            public List<string> AttributePath
            {
                get { return attributePath; }
            }
            private List<string> elementPath = new List<string>();
            public List<string> ElementPath
            {
                get { return elementPath; }

            }
            public string CompositePath
            {
                get { return ParsedXPaths.First().CompositeXPath; }
            }
            public bool IsContainElement
            {
                get
                {
                    return elementPath.Count >= 1;
                }
            }
        }
    }
    
}
